<template>
  <view class="content">
    <uni-list>
      <uni-list-item
        class="font-28"
        :title="`当前${mydata.typeText}`"
        :rightText="`${mydata.num}包`"
      ></uni-list-item>
      <uni-list-item>
        <template v-slot:body>
          <view class="fd">
            <text>接收人ID</text>
            <input v-model="tID" placeholder="请输入接受人ID" />
          </view>
        </template>
      </uni-list-item>
      <uni-list-item>
        <template v-slot:body>
          <view class="fd">
            <text>转赠包数</text>
            <input v-model="num" placeholder="请输入转赠包数" />
          </view>
        </template>
      </uni-list-item>
    </uni-list>
    <text class="mar-top"> 1、一旦转赠后，不可退回； </text>
    <button @click="submit" type="default">确认转赠</button>
  </view>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { onLoad } from "@dcloudio/uni-app";
import { sendUserCoin } from "@/api/bgame";
import { useUserStoreHook } from "@/store/user";
const tID = ref("");
const num = ref("");
const mydata = ref({
  consume_type: "",
  type: "",
  num: 0,
});

/**
 * 
 * 
 * send_user_id:转出用户rece_user_id:转入用户
num：数量
consume_type：类型
 */

const submit = async () => {
  if (!num.value) {
    uni.showToast({
      title: "请输入包数",
      mask: true,
      icon: "error",
    });
    return;
  }
  if (num.value - mydata.value.num > 0) {
    uni.showToast({
      title: "包数过大",
      mask: true,
      icon: "error",
    });
    return;
  }
  uni.showLoading({
    title: "转赠中...",
    mask: true,
  });
  const res = await sendUserCoin({
    send_user_id: uni.getStorageSync("user_id"),
    rece_user_id: tID.value,
    num: num.value,
    consume_type: mydata.value.consume_type,
  });
  uni.hideLoading();
  if (1 == res.result) {
    uni.showToast({
      title: "转赠成功",
      icon: "none",
    });
    goto("/pages/dog/myl/index");
  } else {
    uni.showToast({
      title: res.msg || "转赠失败",
      mask: true,
      icon: "error",
    });
  }
};

const goto = (path) => {
  uni.navigateTo({
    url: path,
  });
};

const typeObj = {
  1: "商家置换沐浴露",
  2: "养狗置换沐浴露",
  3: "分享奖励沐浴露",
  4: "新人赠送沐浴露",
  5: "消费赠送沐浴露",
  6: "商家增值沐浴露",
};
onLoad(async (option) => {
  await useUserStoreHook().getSign();
  console.log("option", option);
  mydata.value.consume_type = option.type || "1";
  mydata.value.typeText = typeObj[option.type] || "商家置换沐浴露";
  mydata.value.num = option.num || "0";
  // getList();
});
</script>

<style lang="scss" scoped>
.content {
  width: 100%;
  height: calc(100vh - 44px);
  padding: 8rpx;
  display: flex;
  flex-direction: column;

  .mar-top {
    margin-top: 30rpx;
  }

  text {
    width: 100%;
    padding: 5rpx;
    margin-top: 5rpx;
  }

  button {
    margin: 30rpx;
    padding: 5rpx;
    color: white;
    background-color: #009900;
  }
}

.fd {
  width: 100%;
  font-size: 28rpx;
  display: flex;
  flex-direction: column;

  input {
    margin-top: 10rpx;
  }
}

.font-28 {
  font-size: 28rpx;
}
</style>
